/*-------------------------------------------------------------------------------------------------*/
/*                                                                                                 */
/*                                                                                                 */
/* Copyright (c) 2008 Nuvoton Technology Corp.                                                     */
/* All rights reserved                                                                             */
/*                                                                                                 */
/*-------------------------------------------------------------------------------------------------*/
/* File Name:                                                                                      */
/*     dns.c                                                                                       */
/* Description:                                                                                    */
/*     This application tests lwip DNS related functions.                                          */
/* History:                                                                                        */
/*                                                                                                 */
/* Project:                                                                                        */
/*-------------------------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "wblib.h"
#include "NUC900_lwip.h"




CHAR host[] = "www.nuvoton.com";

#define USE_DHCP


VOID _InitSystem(VOID)
{

    WB_PLL_T sysClock;

    sysDisableCache();
    sysInvalidCache();
    sysSetMMUMappingMethod(MMU_DIRECT_MAPPING);
    sysEnableCache(CACHE_WRITE_BACK);

    sysClock.pll0 = PLL_166MHZ;
    sysClock.pll1 = PLL_100MHZ;
    sysClock.cpu_src = CPU_FROM_PLL0;
    sysClock.ahb_clk = AHB_CPUCLK_1_2;
    sysClock.apb_clk = APB_AHB_1_2;

    sysSetPLLConfig(&sysClock);

}

INT main(VOID)
{

    struct ip_addr gw, ipaddr, netmask, ans;
#ifndef USE_DHCP
    struct ip_addr dns;
#endif
    struct netif netif;

    _InitSystem();

#ifdef USE_DHCP

    IP4_ADDR(&gw, 0, 0, 0, 0);
    IP4_ADDR(&ipaddr, 0, 0, 0, 0);
    IP4_ADDR(&netmask, 0, 0, 0, 0);


#else

    IP4_ADDR(&gw, 192, 168, 0, 1);
    IP4_ADDR(&ipaddr, 192, 168, 0, 227);
    IP4_ADDR(&netmask, 255, 255, 255, 0);
    IP4_ADDR(&dns, 168, 95, 1, 1);
#endif
    stats_init();
    sys_init();

    /* mem_init(); */
    memp_init();
    pbuf_init();


    etharp_init();

    ip_init();
    udp_init();
    tcp_init();


#ifdef USE_DHCP
    netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input);
    netif_set_default(&netif);
    dhcp_start(&netif);
#pragma O0     /* cannot let compiler optimize following code, or we'll loop forever...    */
    while ((netif.dhcp->state) != DHCP_BOUND); /* wait until we get a ip before send out dns query */

#else
    netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input);
    netif_set_dns(&netif, &dns);
    netif_set_default(&netif);
    netif_set_up(&netif);

#endif

restart:
    sysprintf("send dns query - ");
    if (dns_start(&netif, host, &ans) != ERR_OK) {
        sysprintf("failed, try again\n");
        goto restart;
    } else
        sysprintf("IP address of %s is %d.%d.%d.%d\n", host,
                  (ans.addr) & 0xff,
                  (ans.addr >> 8) & 0xff,
                  (ans.addr >> 16) & 0xff,
                  (ans.addr >> 24) & 0xff);


    return(0);

}